package com.jm3.riscar3.memory;

import com.jm3.riscar3.exceptions.IOPortAddressingException;
import com.jm3.riscar3.exceptions.OutOfBoundsMemoryLocationException;

public class Memory {

	private int size;
	private int[] locations;
	public Memory(int size) {
		this.size = size;
		this.locations = new int[size];
	}

	public void writeToLocation(int memLoc, int data)
			throws OutOfBoundsMemoryLocationException{

		if (memLoc < 0 || memLoc >= this.size) {
			throw new OutOfBoundsMemoryLocationException("Location " + memLoc
					+ " is not addressable: memory size is " + this.size);
		}
		

		locations[memLoc] = data;
	}

	public int readLocation(int memLoc)
			throws OutOfBoundsMemoryLocationException, IOPortAddressingException  {
		if (memLoc < 0 || memLoc >= this.size) {
			throw new OutOfBoundsMemoryLocationException("Location " + memLoc
					+ " is not addressable: memory size is " + this.size);
		}
		
		return locations[memLoc];
	}


}
